﻿using Microsoft.Practices.Prism.StoreApps;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using Sumc.Client.ModernUI.Extensions;
using Sumc.Client.ModernUI.Helpers;
using Sumc.Client.ModernUI.Services;
using Sumc.Models.Responses;
using System;
using System.Collections.ObjectModel;

namespace Sumc.Client.ModernUI.ViewModels
{
    public class ScheduleRightTimeFlyoutViewModel : BaseViewModel, IFlyoutViewModel
    {
        private Action closeFlyout;
        private new Action goBack;
        private string information;
        private string route;
        private Stop stop;
        private DelegateCommand goBackCommand;
        private ObservableCollection<string> rightArrivalTimes;
        private readonly IScheduleRepository scheduleRepository;

        public ScheduleRightTimeFlyoutViewModel(INavigationService navigationService,
            IAlertMessageService alertMessageService,
            IScheduleRepository scheduleRepository,
            ISearchPaneService searchPaneService,
            IFlyoutService flyoutService) : base(navigationService,
            alertMessageService,
            flyoutService,
            searchPaneService)
        {
            this.scheduleRepository = scheduleRepository;
        }

        public DelegateCommand GoBackCommand
        {
            get
            {
                if (this.goBackCommand == null)
                {
                    this.goBackCommand = new DelegateCommand(() => this.closeFlyout());
                }

                return this.goBackCommand;
            }
        }

        public Stop Stop
        {
            get
            {
                return this.stop;
            }

            set
            {
                this.SetProperty(ref this.stop, value);
            }
        }

        public string Information
        {
            get
            {
                return this.information;
            }

            set
            {
                this.SetProperty(ref this.information, value);
            }
        }

        public string Route
        {
            get
            {
                return this.route;
            }

            set
            {
                this.SetProperty(ref this.route, value);
            }
        }


        public ObservableCollection<string> RightArrivalTimes
        {
            get
            {
                return this.rightArrivalTimes;
            }

            set
            {
                this.SetProperty(ref this.rightArrivalTimes, value);
            }
        }

        public Action CloseFlyout
        {
            get
            {
                return this.closeFlyout;
            }

            set
            {
                this.SetProperty(ref this.closeFlyout, value);
            }
        }

        public new Action GoBack
        {
            get
            {
                return this.goBack;
            }

            set
            {
                this.SetProperty(ref this.goBack, value);
            }
        }

        public void Open(object parameter, Action successAction)
        {
            this.LoadData(async () =>
            {
                var rightTime = await this.scheduleRepository.GetTransportVirtualTable(parameter as string);
                this.LoadingData = false;
                this.Information = rightTime.Information;
                this.Route = rightTime.Route;
                this.Stop = rightTime.Stop;
                this.RightArrivalTimes = new ObservableCollection<string>();
                this.RightArrivalTimes.FillCollection(rightTime.RightTimes);
            });
        }
    }
}